/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MyThread;

import MyLib.Comvar;
import MyLib.MyPacket;
import Object.Torrent;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.ArrayList;

/**
 *
 * @author QuangHau
 */
public class FindChunk extends Thread {

    private Torrent torrenFile;

    public FindChunk(Torrent _torrenFile) {
        torrenFile = _torrenFile;
    }

    public void run() {
        try {
            Comvar.writeLog(1, FindChunk.class.getName(), "BEGIN FIND CHUNK OF FILE " + torrenFile.getName() + "  ID: " + torrenFile.getId());
            //khoi tao socket
            DatagramSocket socket = new DatagramSocket();
            //socket.setBroadcast(true);
            int port = 35617;
            //InetAddress ipServer = InetAddress.getByName("127.0.0.1");
            ArrayList<String> listIP = load_IP();
            if (listIP != null) {
                while (!Comvar.stopFindChunk) {
                    for (int i = 0; i < torrenFile.getChunks().size(); i++) {
                        for (int j = 0; j < listIP.size(); j++) {
                            InetAddress ipServer = InetAddress.getByName(listIP.get(j));
                            /*----Tạo data----*/
                            String stSend = torrenFile.getId() + ";" + torrenFile.getChunks().get(i).getId();
                            byte type = 10;
                            byte option = 0;
                            DatagramPacket sendPacket = MyPacket.createPacket(stSend.getBytes(), ipServer, port, type, option, 0, 0);
                            //Send DatagramPacket
                            System.out.println("tim chunk " + stSend);
                            Comvar.writeLog(3, FindChunk.class.getName(), "SEND " + ipServer.getHostAddress() + 
                                        " [Find chunk " + stSend + "]");
                            socket.send(sendPacket);
                        }
                        Thread.sleep(10);
                    }
                    Clock clock = new Clock(10000, Comvar.threadFindChunk);
                    clock.start();
                    Comvar.threadFindChunk.acquire();
                    clock.stopClock();
                }
            }
            //dong DatagramSocket
            socket.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("exit find chunk");
        Comvar.writeLog(1, FindChunk.class.getName(), "END FIND CHUNK OF FILE " + torrenFile.getName() + "  ID: " + torrenFile.getId());
    }

    public void stopFindChunk() {
        Comvar.stopFindChunk = true;
        Comvar.threadFindChunk.release();
    }

    public ArrayList<String> load_IP() {
        ArrayList<String> lst_IP = new ArrayList<String>();
        try {
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream("network.map"));
            int num = (int) ois.readObject();

            for (int i = 0; i < num; i++) {
                lst_IP.add((String) ois.readObject());
            }
            ois.close();
        } catch (Exception e) {
            return null;
        }
        return lst_IP;
    }
}